#include "ocv_table.h"
#include "systemdata.h"
#include <stdio.h>
#include "debug_uart.h"

static const uint16_t lstl_ocv[] = {
	2840,2908,2976,3044,3112,3180,3184,3188,3192,3196,//0-9%
	3200,3204,3207,3211,3215,3219,3223,3227,3231,3236,//10%-19%
	3240,3244,3248,3252,3256,3260,3263,3267,3270,3273,//20%-29%
	3277,3279,3282,3284,3286,3289,3288,3288,3288,3287,//30%-39%
	3287,3287,3287,3287,3288,3288,3288,3288,3288,3289,//40%-49%
	3289,3289,3289,3289,3290,3290,3292,3294,3296,3299,//50%-59%
	3301,3303,3306,3308,3311,3314,3316,3319,3322,3324,//60%-69%
	3327,3327,3327,3327,3328,3328,3328,3328,3328,3328,//70%-79%
	3328,3328,3328,3328,3329,3329,3329,3329,3329,3330,//80%-89%
	3330,3330,3330,3330,3331,3331,3333,3335,3337,3339,3341//90%-100%
};



//static const uint16_t LiNiMnO4_ocv[] = {
//	3367,3382,3397,3412,3428,3443,3454,3465,3476,3487,//0-9%
//	3498,3506,3514,3522,3530,3538,3543,3549,3555,3560,//10%-19%
//	3566,3570,3575,3579,3583,3588,3591,3595,3599,3602,//20%-29%
//	3606,3609,3613,3616,3619,3623,3626,3630,3634,3637,//30%-39%
//	3641,3645,3650,3654,3658,3663,3668,3673,3678,3683,//40%-49%
//	3688,3694,3700,3706,3712,3718,3725,3732,3739,3747,//50%-59%
//	3754,3762,3770,3778,3787,3795,3804,3813,3822,3831,//60%-69%
//	3840,3850,3860,3870,3880,3890,3901,3911,3922,3933,//70%-79%
//	3944,3955,3967,3978,3989,4001,4013,4024,4036,4048,//80%-89%
//	4060,4072,4085,4097,4109,4122,4134,4146,4158,4170,4182//90%-100%
//};


static const uint16_t LiNiMnO4_ocv[] = {
	3104,3201,3268,3328,3392,3479,3530,3568,3609,3665,//0-9%
	3703,3724,3740,3753,3764,3774,3783,3792,3799,3807,//10%-19%
	3813,3819,3824,3829,3834,3839,3843,3847,3851,3855,//20%-29%
	3859,3863,3866,3870,3874,3877,3881,3885,3889,3893,//30%-39%
	3897,3901,3905,3909,3913,3917,3921,3925,3929,3933,//40%-49%
	3937,3941,3945,3949,3953,3958,3962,3966,3970,3973,//50%-59%
	3979,3980,3984,3987,3991,3994,3998,4002,4005,4009,//60%-69%
	4013,4017,4021,4025,4029,4033,4038,4042,4046,4051,//70%-79%
	4055,4060,4064,4070,4076,4081,4086,4091,4096,4102,//80%-89%
	4108,4114,4121,4128,4135,4143,4152,4161,4168,4178,4196//90%-100%
};


static const uint16_t sanyuan_ocv[] = {
	3062,3074,3086,3098,3100,3123,3146,3169,3192,3215,//0-9%
	3237,3268,3299,3330,3361,3391,3401,3411,3421,3431,//10%-19%
	3440,3453,3466,3479,3492,3506,3516,3526,3536,3546,//20%-29%
	3556,3563,3570,3576,3582,3588,3594,3599,3604,3609,//30%-39%
	3614,3620,3625,3630,3635,3640,3646,3653,3660,3667,//40%-49%
	3674,3683,3693,3703,3713,3723,3734,3746,3758,3770,//50%-59%
	3782,3794,3806,3818,3830,3843,3853,3863,3873,3883,//60%-69%
	3893,3902,3911,3919,3927,3936,3946,3956,3966,3977,//70%-79%
	3988,3998,4008,4019,4030,4041,4049,4056,4063,4070,//80%-89%
	4077,4083,4088,4093,4098,4103,4118,4133,4148,4163,4179//90%-100%
};

static const uint16_t NaFR_OCV[] = {
	1854,1978,2056,2116,2168,2215,2259,2299,2337,2374,//0-9%
	2409,2443,2476,2508,2540,2569,2598,2624,2649,2671,//10%-19%
	2691,2710,2725,2740,2752,2763,2773,2783,2790,2797,//20%-29%
	2803,2810,2815,2821,2826,2830,2835,2840,2848,2858,//30%-39%
	2870,2884,2897,2911,2926,2941,2957,2973,2988,3004,//40%-49%
	3020,3036,3052,3069,3085,3101,3119,3136,3154,3171,//50%-59%
	3189,3208,3225,3244,3261,3279,3297,3315,3332,3349,//60%-69%
	3366,3384,3400,3416,3431,3446,3460,3475,3489,3503,//70%-79%
	3517,3530,3544,3558,3572,3586,3600,3615,3630,3646,//80%-89%
	3661,3678,3696,3715,3734,3755,3778,3802,3828,3855,3853//90%-100%
};

static SysConfData_t *confinfo = NULL;//系统参数指针
static SysRunData_t  *runinfo = NULL;//系统运行数据指针

static uint8_t ocvToSoc(uint16_t *ocv_tab){
	uint16_t *pOcvTable =NULL;
	switch(confinfo->fctData.batType){
		case sanyuanBat:{pOcvTable = (uint16_t *)sanyuan_ocv;}break;//三元锂电池
		case lstlBat:{pOcvTable = (uint16_t *)lstl_ocv;}break;//磷酸铁锂电池
		case mslBat:{pOcvTable = (uint16_t *)LiNiMnO4_ocv;}break;//锰酸锂电池
		case gslBat:{}break;//钴酸锂电池
		case tslBat:{}break;//钛酸锂电池
		case nslBat:{pOcvTable = (uint16_t *)NaFR_OCV;}break;
	}
	if(runinfo->batMinVol < pOcvTable[0]){
		//trace_info("runinfo->rmCapEmptyFlag = true\n");
		runinfo->rmCapEmptyFlag = true;
		return 0;
	}else{
		uint16_t i = 0;
		for(i=1;i<101;i++){
			if(runinfo->batMinVol < pOcvTable[i]){
				break;
			}
		}
		return i-1;
	}
	return 0;
}

uint16_t socToOcv(uint8_t soc){
	switch(confinfo->fctData.batType){
		case sanyuanBat:{
			return sanyuan_ocv[soc%101];
		}break;//三元锂电池
		case lstlBat:{
			return lstl_ocv[soc%101];
		}break;//磷酸铁锂电池
		case mslBat:{//锰酸锂电池
			return LiNiMnO4_ocv[soc%101];
		}break;
		case gslBat:{}break;//钴酸锂电池
		case tslBat:{}break;//钛酸锂电池
		case nslBat:{
			return NaFR_OCV[soc%101];
		}break;
	}
	return 0;
}

void updateOcvSoc(void){
	//trace_info("runinfo->batMinVol = %d\n",runinfo->batMinVol);
	switch(confinfo->fctData.batType){
		case sanyuanBat:{
			runinfo->batSoc = ocvToSoc((uint16_t *)sanyuan_ocv);
		}break;//三元锂电池
		case lstlBat:{
			if(runinfo->batMinVol <= lstl_ocv[30] || runinfo->batMinVol >= lstl_ocv[95]){
				runinfo->batSoc = ocvToSoc((uint16_t *)lstl_ocv);
			}else{
				printf("lstl ocv compute,do nothing!\n");
			}
		}break;//磷酸铁锂电池
		case mslBat:{//锰酸锂电池
			runinfo->batSoc = ocvToSoc((uint16_t *)LiNiMnO4_ocv);
		}break;
		case gslBat:{}break;//钴酸锂电池
		case tslBat:{}break;//钛酸锂电池
		case nslBat:{runinfo->batSoc = ocvToSoc((uint16_t *)NaFR_OCV);}break;
	}
	runinfo->rmCap_mAms = runinfo->realCap_mAms*runinfo->batSoc/100;//剩余容量初始化
}

void firstUpdateOcvSoc(void){
	static bool socInitFlag = false;
	if(socInitFlag == true){
		return;
	}else{
		socInitFlag = true;
	}
	printf("runinfo->batMinVol = %d\n",runinfo->batMinVol);
	switch(confinfo->fctData.batType){
		case sanyuanBat:{
			runinfo->batSoc = ocvToSoc((uint16_t *)sanyuan_ocv);
		}break;//三元锂电池
		case lstlBat:{
			runinfo->batSoc = ocvToSoc((uint16_t *)lstl_ocv);
		}break;//磷酸铁锂电池
		case mslBat:{//锰酸锂电池
			runinfo->batSoc = ocvToSoc((uint16_t *)LiNiMnO4_ocv);
		}break;
		case gslBat:{}break;//钴酸锂电池
		case tslBat:{}break;//钛酸锂电池
		case nslBat:{runinfo->batSoc = ocvToSoc((uint16_t *)NaFR_OCV);}break;
	}
	runinfo->rmCap_mAms = runinfo->realCap_mAms*runinfo->batSoc/100;//剩余容量初始化
}

void ocv_soc_init(void){
	confinfo = getSysConfData();
	runinfo = getSysRunData();
//	switch(confinfo->fctData.batType){
//		case sanyuanBat:{runinfo->batSoc = ocvToSoc((uint16_t *)sanyuan_ocv);}break;//三元锂电池
//		case lstlBat:{runinfo->batSoc = ocvToSoc((uint16_t *)lstl_ocv);}break;//磷酸铁锂电池
//		case mslBat:{}break;//锰酸锂电池
//		case gslBat:{}break;//钴酸锂电池
//		case tslBat:{}break;//钛酸锂电池
//	}
}